import ImagePicker from "react-native-image-picker";
import NetworkUrl from '../common/NetworkUrlUtils';
import {
    Linking,
    Platform,
    Alert
} from 'react-native';
var img_path = "file:///storage/emulated/0/DCIM/";
var img_id = "165413213456416";
export default class ImagePick {
    static showImage(_this){
        let uploadImage = function(url,params){
            let formData = new FormData();
            for (var key in params){
                formData.append(key, params[key]);
            }
            let file = {uri: params.path, type: 'application/octet-stream', name: 'image.jpg'};
            formData.append("uploadFileName", file);
            fetch( url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'multipart/form-data;charset=utf-8',
                    "x-access-token": '4c0b2e8c8c094e5eadfea3029fa2fb0e',
                },
                body: formData,
            }).then((response) => response.json())
                .then((responseData)=> {
                    let _res =  responseData
                    let datas= JSON.parse(_res.datas);
                    if(_res.status == 200){
                        img_id = datas.id;
                        let data = {
                            "path":img_path,
                            "id":img_id
                        }
                        _this.getImgList(data)
                    }
                    else{
                        Alert.alert('提示',_res.msg);
                    }

                }).catch((err)=> {
                reject(err);
            });
        }
        let options = {
            title: '选择图片',
            cancelButtonTitle: '取消',
            takePhotoButtonTitle: '拍照',
            chooseFromLibraryButtonTitle: '图片库',
            cameraType: 'back',
            mediaType: 'photo',
            videoQuality: 'high',
            durationLimit: 10,
            maxWidth: 600,
            maxHeight: 600,
            aspectX: 2,
            aspectY: 1,
            quality: 0.8,
            angle: 0,
            allowsEditing: false,
            noData: false,
            storageOptions: {
                skipBackup: true,
                path: 'images'
            }
        };
        ImagePicker.showImagePicker(options,(response) => {
            if (response.didCancel) {
                console.log('User cancelled image picker');
            } else if (response.error) {
                let url=""
                let text=""
                if(response.error.indexOf("Photo")>-1){
                    text = "请允许应用访问您的手机相册";
                }else{
                    text = "请允许应用访问您的相机";
                }

                Alert.alert('提示',text,
                    [
                        {
                            text: '确定',
                        },
                    ],
                    {
                        cancelable: true,
                    });

            }

            else {

                let source;

                if (Platform.OS === 'android') {
                    source = {uri: response.uri, isStatic: true}
                } else {
                    source = {uri: response.uri.replace('file://', ''), isStatic: true}
                }
                if (Platform.OS === 'android') {
                    img_path = response.uri
                } else {
                    img_path = response.uri.replace('file://', '')
                }
                _this.setState({
                    uploadfile:img_path
                });
               uploadImage(NetworkUrl.ImageUpload,{"path":img_path,"roleId":"1"});
            }
        });
    }
}